﻿using System;
using System.Collections.Generic; 
using System.Reflection;

namespace Titan
{
    /// <summary>
    /// 封装一些常用函数
    /// </summary>
    public static class Util
    {
        public static DateTime DefaultDateTime = new DateTime();

        private static readonly Dictionary<Type, object> Caches = new Dictionary<Type, object>();
        private static readonly MethodInfo GetDefaultValueMethod = typeof(Util).GetMethod("D", BindingFlags.Static | BindingFlags.NonPublic);


        private static T D<T>()
        {
            return default(T);
        }


        public static object GetDefaultValue(Type type)
        {
            if (!Caches.ContainsKey(type))
            {
                lock (Caches)
                {
                    if (!Caches.ContainsKey(type))
                    {
                        MethodInfo getDefaultValueMethodGeneric = GetDefaultValueMethod.MakeGenericMethod(type);
                        object value = getDefaultValueMethodGeneric.Invoke(null, new object[] { });
                        Caches.Add(type, value);
                    }
                }
            }
            return Caches[type];
        }

        //public static Type GetDeclaredType<T>(T self)
        //{
        //    return typeof(T);
        //}

        ///// <summary>
        ///// 从某个集合中排除一些对象，不区分大小写
        ///// </summary>
        ///// <param name="original">原始对象集合</param>
        ///// <param name="exclusion">需要排除的对象</param>
        ///// <returns></returns>
        //public static IEnumerable<string> Exclude(string[] original, IEnumerable<string> exclusion)
        //{
        //    if (exclusion == null) return original;

        //    HashSet<string> exclusionHash = new HashSet<string>(exclusion,StringComparer.OrdinalIgnoreCase);//不区分大小写
        //    if (exclusionHash.Count == 0) return original;

        //    List<string> list = new List<string>(original.Length);
        //    foreach (string s in original)
        //    {
        //        if (!exclusionHash.Contains(s))
        //        {
        //            list.Add(s);
        //        }
        //    }
        //    return list;
        //}

        public static IConditionCollection ToConditionCollection(IConditionExpression conditionExpression)
        {
            if (conditionExpression is IConditionCollection)
            {
                return conditionExpression as IConditionCollection;
            }
            IConditionCollection conditionCollection = new ConditionCollection();
            conditionCollection.Add(conditionExpression);
            return conditionCollection;
        }

        

        /// <summary>
        /// 检查pageSize和pageIndex 是否合法
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        public static void CheckPageSizeAndPageIndex(int pageSize, int pageIndex)
        {
            if (pageSize < 0) throw ExceptionFactory.PageSizeArgumentOutOfRangeException();
            if (pageIndex < 1) throw ExceptionFactory.PageIndexArgumentOutOfRangeException();
        }



        public static string ToGroupPropertyKey(string propertyName, GroupFunction groupFunction)
        {
            return propertyName + "_" + groupFunction.ToString();
        }


         
    }
}
